package com.geping.etl.common.service.impl;


import com.geping.etl.common.entity.DataDictionary;
import com.geping.etl.common.repository.DataDictionaryRepository;
import com.geping.etl.common.service.DataDictionaryService;
import com.geping.etl.utils.common.commonsql.MatchType;
import com.geping.etl.utils.common.commonsql.ParamUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

    @Autowired
    private DataDictionaryRepository dataDictionaryRepository;

    @Autowired
    private HttpServletRequest request;

    @Override
    public List<DataDictionary> findByType(Integer type) {
        Specification specification = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("type"),type));
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.ASC, "px");
        return  dataDictionaryRepository.findAll(specification,sort);
    }

    @Override
    public List<DataDictionary> findByType_param(Integer type, String q) {
        Specification specification = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("type"),type));
            if(StringUtils.isNotBlank(q)){
                predicates.add(criteriaBuilder.like(root.get("text"),'%'+q+'%'));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.ASC, "px");
        return  dataDictionaryRepository.findAll(specification,sort);
    }


    @Override
    public List<DataDictionary> getDataDictionary() {
        return dataDictionaryRepository.findAll();
    }

    @Override
    public Map<Integer, Map<String, String>> initDataDictionaryKV() {
        Map<Integer, Map<String, String>>dcMap=new HashMap<>();
        List<DataDictionary> dataDictionarys=getDataDictionary();
        List<Integer>types=getDataDictionaryTypes(dataDictionarys);
        for(Integer type:types){
                dcMap.put(type,getTypeItem(type,dataDictionarys));
        }
        return dcMap;
    }

    /**
     * 找到type对应的map
     * @param dataDictionarys
     * @return
     */
    private Map<String, String> getTypeItem(Integer type,List<DataDictionary> dataDictionarys) {
        Map<String, String> typeMap=new HashMap<>();
        for(DataDictionary dataDictionary:dataDictionarys){
            if(dataDictionary.getType().intValue()==type.intValue()){
                typeMap.put(dataDictionary.getCode(),dataDictionary.getText());
            }
        }
        return typeMap;
    }

    /**
     * 获取数据字典所有的类型
     * @param dataDictionarys
     * @return
     */
    private List<Integer> getDataDictionaryTypes(List<DataDictionary> dataDictionarys) {
        List<Integer>types=new ArrayList<>();
        for(DataDictionary dataDictionary:dataDictionarys){
            if(types.contains(dataDictionary.getType())){
                continue;
            }
            types.add(dataDictionary.getType());
        }
       return types;
    }
}